﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Bussiness;
using System.IO;

namespace ShoppingCartWEB.Admin
{
    public partial class AddNewProduct : System.Web.UI.Page
    {
        ProductManager proMan = new ProductManager();
        CategoryManager cateMan = new CategoryManager();
        SupplierManager supMan = new SupplierManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadSupplier();
                loadCategory();
            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                if (flImage.HasFile)
                {
                    try
                    {
                        Product pr = new Product();
                        pr.ProductName = txtName.Text;
                        pr.CategoryID = Convert.ToInt32(ddlCategory.SelectedItem.Value);
                        pr.SupplierID = Convert.ToInt32(ddlSupplier.SelectedItem.Value);
                        pr.ProductPrice = Convert.ToDecimal(txtPrice.Text);
                        pr.Unit = txtUnit.Text;
                        pr.Quantity = Convert.ToInt32(txtQuantity.Text);
                        pr.Decription = txtDecription.Text;
                        pr.isActive = true;
                        string sPath = Server.MapPath("~/web.config");
                        sPath = sPath.Substring(0, sPath.LastIndexOf("web.config"));
                        pr.imageLink = "~/Images/Products/" + flImage.FileName;
                        sPath = sPath + pr.imageLink;
                        //pr.imageLink = sPath;
                        flImage.SaveAs(sPath);
                        if (!proMan.AddNewProduct(pr))
                        {
                            Response.Redirect("~/ErrorMessage.aspx?message= Cannot save image");
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("~/ErrorMessage.aspx?message=" + ex.Message);
                    }
                    
                    
                }
            }

        }

        public void loadSupplier()
        {
            ddlSupplier.DataSource = supMan.AllSupplier();
            ddlSupplier.DataValueField = "SupplierID";
            ddlSupplier.DataTextField = "CompanyName";
            ddlSupplier.DataBind();
        }

        public void loadCategory()
        {
            ddlCategory.DataSource = cateMan.AllCategory();
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }
    }

}